@echo off
echo ================================
echo    考试管理系统 - Windows部署脚本
echo ================================

:: 设置变量
set NGINX_DIR=E:\Program Files (x86)\nginx-1.25.3
set PROJECT_DIR=%~dp0
set FRONTEND_ADMIN_DIR=%PROJECT_DIR%frontend-admin
set FRONTEND_H5_DIR=%PROJECT_DIR%frontend-h5
set BACKEND_DIR=%PROJECT_DIR%backend

echo 当前项目目录: %PROJECT_DIR%
echo Nginx安装目录: %NGINX_DIR%

:: 检查Nginx是否存在
if not exist "%NGINX_DIR%\nginx.exe" (
    echo 错误: 未找到Nginx，请先安装Nginx到 %NGINX_DIR%
    pause
    exit /b 1
)

:: 创建必要的目录
echo 创建目录结构...
if not exist "%NGINX_DIR%\html\frontend-admin" mkdir "%NGINX_DIR%\html\frontend-admin"
if not exist "%NGINX_DIR%\html\frontend-h5" mkdir "%NGINX_DIR%\html\frontend-h5"
if not exist "%NGINX_DIR%\logs" mkdir "%NGINX_DIR%\logs"

:: 打包前端应用
echo.
echo ================================
echo 打包管理员前端...
echo ================================
cd /d "%FRONTEND_ADMIN_DIR%"
call npm install
if errorlevel 1 (
    echo 错误: 管理员前端依赖安装失败
    pause
    exit /b 1
)

call npm run build-only
if errorlevel 1 (
    echo 错误: 管理员前端打包失败
    pause
    exit /b 1
)

:: 复制管理员前端文件
echo 复制管理员前端文件到Nginx目录...
if exist "%FRONTEND_ADMIN_DIR%\dist" (
    xcopy "%FRONTEND_ADMIN_DIR%\dist\*" "%NGINX_DIR%\html\frontend-admin\" /E /Y /I
    echo 管理员前端文件复制完成
) else (
    echo 错误: 管理员前端dist目录不存在
    pause
    exit /b 1
)
xcopy /s /e /y "%FRONTEND_ADMIN_DIR%\dist\*" "%NGINX_DIR%\html\frontend-admin\"

echo.
echo ================================
echo 打包H5移动端...
echo ================================
cd /d "%FRONTEND_H5_DIR%"
call npm install
if errorlevel 1 (
    echo 错误: H5前端依赖安装失败
    pause
    exit /b 1
)

call npm run build
if errorlevel 1 (
    echo 错误: H5前端打包失败
    pause
    exit /b 1
)

:: 复制H5前端文件
echo 复制H5前端文件到Nginx目录...
xcopy /s /e /y "%FRONTEND_H5_DIR%\dist\*" "%NGINX_DIR%\html\frontend-h5\"

:: 安装后端依赖
echo.
echo ================================
echo 安装后端依赖...
echo ================================
cd /d "%BACKEND_DIR%"
call npm install
if errorlevel 1 (
    echo 错误: 后端依赖安装失败
    pause
    exit /b 1
)

:: 安装PM2进程管理器
echo.
echo ================================
echo 安装PM2进程管理器...
echo ================================
call npm list -g pm2 >nul 2>&1
if errorlevel 1 (
    echo 安装PM2...
    call npm install -g pm2
    if errorlevel 1 (
        echo 警告: PM2安装失败，将使用普通方式启动
    ) else (
        echo ✓ PM2安装成功
    )
) else (
    echo ✓ PM2已安装
)

:: 创建日志目录
if not exist "%PROJECT_DIR%logs" mkdir "%PROJECT_DIR%logs"

:: 复制Nginx配置
echo.
echo ================================
echo 配置Nginx...
echo ================================
copy /y "%PROJECT_DIR%conf\nginx.conf" "%NGINX_DIR%\conf\nginx.conf"

:: 测试Nginx配置
echo 测试Nginx配置...
cd /d "%NGINX_DIR%"
nginx.exe -t
if errorlevel 1 (
    echo 错误: Nginx配置文件有误
    pause
    exit /b 1
)

echo.
echo ================================
echo 部署完成！
echo ================================
echo 接下来请手动执行以下步骤:
echo.
echo 方式一: 使用PM2管理后端服务（推荐）
echo    cd %PROJECT_DIR%
echo    pm2 start ecosystem.config.json
echo    pm2 startup （设置开机自启）
echo    pm2 save
echo.
echo 方式二: 普通方式启动后端
echo    cd %BACKEND_DIR%
echo    npm start
echo.
echo 启动Nginx:
echo    cd %NGINX_DIR%
echo    nginx.exe
echo.
echo 或者直接运行: start-services.bat
echo.
echo 访问应用:
echo    管理员前端: http://localhost/
echo    H5移动端: http://localhost/h5/
echo    后端API: http://localhost/api/
echo.
pause
